﻿using Fabrikam.Client.ViewModels;
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Connectivity;

namespace Fabrikam.Client
{
    public class Context : ViewModelBase
    {

        private static ItemsPageViewModel itemsPageViewModel;
        private static SettingFlyoutItemViewModel settingFlyoutItemViewModel;
        private static SplitPageViewModel splitPageViewModel;

        public Context()
        {
            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
            this.UpdateConnectionProfile();
        }
        private static Context current;
        public static Context Current
        {
            get
            {
                if (current == null)
                    current = new Context();

                return current;
            }
        }

 
        private NetworkConnectivityLevel connectivityLevel;

        public NetworkConnectivityLevel ConnectivityLevel
        {
            get
            {
                return connectivityLevel;
            }
            set
            {
                if (value == connectivityLevel)
                    return;

                connectivityLevel = value;

                base.RaisePropertyChanged("ConnectivityLevel");
            }
        }

        private void UpdateConnectionProfile()
        {
            var connectionProfile = NetworkInformation.GetInternetConnectionProfile();

            if (connectionProfile != null)
                this.ConnectivityLevel = connectionProfile.GetNetworkConnectivityLevel();
            else
                this.ConnectivityLevel = NetworkConnectivityLevel.None;

        }

        private void NetworkInformation_NetworkStatusChanged(object sender)
        {
            Context.Current.UpdateConnectionProfile();
        }
        public static SettingFlyoutItemViewModel SettingFlyoutItemViewModel
        {
            get
            {
                if (settingFlyoutItemViewModel != null)
                    return settingFlyoutItemViewModel;

                settingFlyoutItemViewModel = (SettingFlyoutItemViewModel)App.Current.Resources["SettingFlyoutItemViewModel"];

                return settingFlyoutItemViewModel;

            }
        }
        public static SplitPageViewModel SplitPageViewModel
        {
            get
            {
                if (splitPageViewModel != null)
                    return splitPageViewModel;

                splitPageViewModel = (SplitPageViewModel)App.Current.Resources["SplitPageViewModel"];

                return splitPageViewModel;
            }
        }
        public static ItemsPageViewModel ItemsPageViewModel
        {
            get
            {
                if (itemsPageViewModel != null)
                    return itemsPageViewModel;

                itemsPageViewModel = (ItemsPageViewModel)App.Current.Resources["ItemsPageViewModel"];

                return itemsPageViewModel;
            }
        }

    }
}
